import random
import ddddocr
import os
import json
import logging

root_path = os.path.dirname(os.path.abspath(__file__)).split("utils")[0]
asset_path = os.path.join(root_path, "asset")
schedule_json_path = os.path.join(asset_path, "schedule.json")


def random_seconds(lower, higher):
    return random.randint(lower, higher)


def init():
    if not os.path.exists(schedule_json_path):
        default = {
            'CurrentCourseName': None,
            'CourseNodeName': None
        }
        with open(schedule_json_path, "w", encoding="utf-8") as f:
            f.write(json.dumps(default, ensure_ascii=False))
        return default
    else:
        with open(schedule_json_path, "r", encoding="utf-8") as f:
            return json.load(f)

def create_loger(name):
    logs_path = root_path + "/logs"
    if not os.path.exists(logs_path):
        os.makedirs(logs_path)
    logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] -%(message)s", filename=f"../logs/{name}.log", filemode="a", encoding="utf-8")
    loger = logging.getLogger()
    return loger

def update_schedule(n1, n2):
    data = {
        'CurrentCourseName': n1,
        'CourseNodeName': n2
    }
    with open(schedule_json_path, "w", encoding='utf-8') as f:
        f.write(json.dumps(data, ensure_ascii=False))


def VerCodeOcr(img_bytes):
    ocr = ddddocr.DdddOcr(show_ad=False)
    result = ocr.classification(img_bytes)
    return result